package org.onepiece.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.onepiece.admin.common.utils.CommonPage;
import org.onepiece.admin.common.utils.CommonResult;
import org.onepiece.admin.component.kafka.KafkaComponent;
import org.onepiece.admin.entity.BaseEntity;
import org.onepiece.admin.entity.onepiece.SysUser;
import org.onepiece.admin.entity.vo.SysUserVO;
import org.onepiece.admin.integration.service.SysUserService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 系统用户表 前端控制器
 * </p>
 *
 * @author hautxxxyzjk@163.com
 * @since 2022-08-24 22:22:16
 */
@RestController
@RequestMapping("/sys/sys-user")
public class SysUserController extends BaseController {

    @Value("${onepiece.kafka-topic.onepiece-user-add-topic}")
    private String userAddTopic;

    private final SysUserService sysUserService;
    private final KafkaComponent kafkaComponent;

    public SysUserController(SysUserService sysUserService, KafkaComponent kafkaComponent) {
        this.sysUserService = sysUserService;
        this.kafkaComponent = kafkaComponent;
    }

    @ApiOperation("保存")
    @PostMapping("/save")
    public CommonResult<Boolean> save() {
        SysUser sysUser = new SysUser();
        sysUser.setUsername("admin" + UUID.randomUUID().toString().replaceAll("-", ""));
        sysUser.setPassword("admin" + UUID.randomUUID().toString().replaceAll("-", ""));
        sysUser.setAge((int) (Math.random() * 100));
        sysUser.setBirthday(LocalDateTime.now());
        boolean save = sysUserService.save(sysUser);

        kafkaComponent.send(userAddTopic, sysUser.toString());

        return CommonResult.ok(save);
    }

    @ApiOperation("保存or编辑")
    @PostMapping("/saveOrUpdate")
    public CommonResult<Boolean> saveOrUpdate() {
        SysUser sysUser = new SysUser();
        sysUser.setUsername("admin" + UUID.randomUUID().toString().replaceAll("-", ""));
        sysUser.setPassword("admin" + UUID.randomUUID().toString().replaceAll("-", ""));
        sysUser.setAge((int) (Math.random() * 100));
        sysUser.setBirthday(LocalDateTime.now());
        boolean save = sysUserService.saveOrUpdate(sysUser);

        return CommonResult.ok(save);
    }

    @ApiOperation("修改")
    @PutMapping("/update/{id}")
    public CommonResult<Boolean> update(@PathVariable Integer id) {
        SysUser sysUser = sysUserService.getById(id);
        sysUser.setId(id);
        sysUser.setAge((int) (Math.random() * 100));
        boolean update = sysUserService.updateById(sysUser);
        return CommonResult.ok(update);
    }

    @ApiOperation("删除")
    @DeleteMapping("/delete/{id}")
    public CommonResult<Boolean> delete(@PathVariable Integer id) {
        boolean remove = sysUserService.removeById(id);
        return CommonResult.ok(remove);
    }

    @ApiOperation("用户列表")
    @GetMapping("/list")
    public CommonResult<List<SysUser>> list() {
        List<SysUser> list = sysUserService.list();
        return CommonResult.ok(list);
    }


    @ApiOperation("用户列表xml")
    @GetMapping("/list-xml")
    public CommonResult<List<SysUser>> listXml() {
        List<SysUser> list = sysUserService.listXml();
        return CommonResult.ok(list);
    }

    @ApiOperation("用户分页")
    @GetMapping("/page")
    public CommonResult<CommonPage<SysUser>> page(@RequestParam(value = "current", defaultValue = "1") Integer current,
                                            @RequestParam(value = "size", defaultValue = "10") Integer size) {
        Page<SysUser> sysUserPage = new Page<>(current, size);

        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(BaseEntity::getId);

        Page<SysUser> page = sysUserService.page(sysUserPage, wrapper);
        CommonPage<SysUser> commonPage = CommonPage.build(page);
        return CommonResult.ok(commonPage);
    }

    @ApiOperation("CompletableFuture")
    @GetMapping("/completable-future")
    public CommonResult<List<SysUserVO>> completableFuture() {
        List<SysUserVO> list = sysUserService.completableFuture();
        return CommonResult.ok(list);
    }
}

